﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.SharePoint.Client;
using System.Net;

namespace SPColumnManager
{
    class ColumnPropertyEditor
    {
        ClientContext ctx;
        string listTitle;
        
        public ColumnPropertyEditor() { }

        public ColumnPropertyEditor(string url)
        {
            ctx = new ClientContext(url);
        }

        public void changeCredentials(string userName, string password)
        {
            ctx.Credentials = new NetworkCredential(userName, password);
        }

        public ListCollection getListCollection()
        {
            Web web = ctx.Web;
            ctx.Load(web.Lists);
            ctx.ExecuteQuery();

            return web.Lists;
        }

        public FieldCollection getListFieldCollection(string listName)
        {
            
            Web web = ctx.Web;
            List list = web.Lists.GetByTitle(listName);
            ctx.Load(list.Fields);
            ctx.ExecuteQuery();

            listTitle = listName;

            return list.Fields;
        }

        public void setPropertyValue(string fieldName, string columnProperty, bool value)
        {
            Web web = ctx.Web;
            List list = web.Lists.GetByTitle(listTitle);
            Field field = list.Fields.GetByInternalNameOrTitle(fieldName);

            switch (columnProperty)
            {
                case "ShowInNewForm":
                    field.SetShowInNewForm(value);
                    ctx.ExecuteQuery();
                    break;
                case "ShowInEditForm":
                    field.SetShowInEditForm(value);
                    ctx.ExecuteQuery();
                    break;
                case "ShowInViewForm":
                    field.SetShowInDisplayForm(value);
                    ctx.ExecuteQuery();
                    break;
                default:
                    break;
            }
        }


    }
}
